package com.lottery.ruok.lottrey.ui.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lottery.ruok.lottrey.R;
/*
 *create by lm   on  2018/8/3
 *describe:普通长龙tablayout自定义view
 */


public class TabSelect extends LinearLayout implements View.OnClickListener {

    private TextView tvTab1;
    private TextView tvTab2;
    private TabSelectListener mTabSelectListener;


    public TabSelect(Context context) {
        super(context);
        init(context);
    }

    public TabSelect(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public TabSelect(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }


    private void init(Context context) {
        LinearLayout tabSelect = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.layout_tab_select, this,true);
        tvTab1 = tabSelect.findViewById(R.id.tv_tab1);
        tvTab2 = tabSelect.findViewById(R.id.tv_tab2);
        tvTab1.setOnClickListener(this);
        tvTab2.setOnClickListener(this);
        tvTab1.setBackgroundResource(R.drawable.tab1_selected_bg);
        tvTab2.setBackgroundResource(R.drawable.tab2_normal_bg);
        tvTab1.setTextColor(getResources().getColor(R.color.bg_white));
        tvTab2.setTextColor(getResources().getColor(R.color.tab_normal));
    }

    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.tv_tab1) {
            tvTab1.setBackgroundResource(R.drawable.tab1_selected_bg);
            tvTab2.setBackgroundResource(R.drawable.tab2_normal_bg);
            if (mTabSelectListener != null) {
                mTabSelectListener.tabOnClick(TabSelectListener.SELECT_TAB1);
            }

            tvTab1.setTextColor(getResources().getColor(R.color.bg_white));
            tvTab2.setTextColor(getResources().getColor(R.color.tab_normal));
        } else if (i == R.id.tv_tab2) {
            tvTab1.setBackgroundResource(R.drawable.tab1_normal_bg);
            tvTab2.setBackgroundResource(R.drawable.tab2_selected_bg);
            if (mTabSelectListener != null) {
                mTabSelectListener.tabOnClick(TabSelectListener.SELECT_TAB2);
            }

            tvTab2.setTextColor(getResources().getColor(R.color.bg_white));
            tvTab1.setTextColor(getResources().getColor(R.color.tab_normal));
        }
    }
    public interface TabSelectListener{
         int SELECT_TAB1 = 1;
        int SELECT_TAB2 = 2;
        void tabOnClick(int selected);
    }
    public void setTabSelectListener(TabSelectListener tabSelectListener) {
        mTabSelectListener = tabSelectListener;
    }
}
